package org.eoti.ai.bayesian;

import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;

public class BayesianNetwork<DATA>
extends BayesianRule<DATA>
{
    private ConcurrentHashMap<BayesianRule<DATA>,Double> rules;

    public BayesianNetwork()
    {
        rules = new ConcurrentHashMap<BayesianRule<DATA>,Double>();
    }

    public void addRule(BayesianRule<DATA> rule, double decidingFactor)
    {
        rules.put(rule, decidingFactor);
    }
    
    @Override
    public void train(DATA data, boolean isGoodData)
    {
        for(BayesianRule<DATA> rule : rules.keySet())
            rule.train(data, isGoodData);
    }

    @Override
    protected double probability(DATA data)
    {
        double pos = 1.0;
        double neg = 1.0;

        for(BayesianRule<DATA> rule : rules.keySet())
        {
            double decidingFactor = rules.get(rule);
            double probability = rule.probability(data);
            pos *= (probability * decidingFactor);
            neg *= ((1-probability)*decidingFactor);
        }

        return pos / (pos+neg);
    }
}
